﻿using Caiji.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Caiji.UserControls
{
    /// <summary>
    /// Reach.xaml 的交互逻辑
    /// </summary>
    public partial class Reach : UserControl
    {
        public Reach()
        {
            InitializeComponent();
            SizeChanged += OnSizeChanged;
        }



        //依赖属性
        public List<ReachCanvas> ItemSource
        {
            get { return (List<ReachCanvas>)GetValue(ItemSourcePropertyProperty); }
            set { SetValue(ItemSourcePropertyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourcePropertyProperty =
            DependencyProperty.Register("ItemSource",
                typeof(List<ReachCanvas>), 
                typeof(Reach));
        //界面发生变化的时候调用
        void Drag()
        {
            //判断是否有数据
            if (ItemSource == null || ItemSource.Count == 0) {
                return;
            }
            //清除之前画的图案
            mainCanvas.Children.Clear();
            p1.Points.Clear();
            p2.Points.Clear();
            p3.Points.Clear();
            p4.Points.Clear();
            p5.Points.Clear();

            //调整大小，正方形，这里 RenderSize值得是 groupbox的 大小,一般指 页面的大小
            double size = Math.Min(RenderSize.Width,RenderSize.Height);
            grid.Height = size;
            grid.Width = size;
            //雷达圆形的半径
            double raduis = size/2;
            //步子 跨度
            double step = 360/ItemSource.Count;
            for (int i = 0; i < ItemSource.Count; i++) {
                var x = raduis + (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180);
                var y = raduis + (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180);
                //找到x,y的坐标
                p1.Points.Add(new Point(x,y));
                p2.Points.Add(new Point(x*0.75,y * 0.75));
                p3.Points.Add(new Point(x * 0.5,y* 0.5));
                p4.Points.Add(new Point(x* 0.25,y * 0.25));
                p5.Points.Add(new Point(raduis + x * ItemSource[i].Value *0.01, raduis + y * ItemSource[i].Value * 0.01));
                //TextBox textBox = new TextBox();
                //textBox.Width = 60;
                //textBox.FontSize = 10;
                //textBox.HorizontalAlignment = HorizontalAlignment.Center;
                //textBox.Text = ItemSource[i].Name;
                ////textBox.Foreground = new SolidColorBrush(Color.FromArgb(255,255,255,255));
                //textBox.Foreground = Brushes.Transparent;
                //计算左边间距
                //textBox.SetValue(Canvas.LeftProperty,raduis+(raduis-10)*Math.Cos((step*i-90)*Math.PI / 180) - 20);
                
                ////计算上边间距
                //textBox.SetValue(Canvas.TopProperty, raduis + (raduis - 10) * Math.Sin((step * i - 90) * Math.PI / 180 - 7) );
                //mainCanvas.Children.Add(textBox);
            }

        }
        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Drag();
        }
    }
}
